import axios from 'axios';
import { ElMessage  } from 'element-plus';

const apiClient = axios.create({
  baseURL: 'http://localhost:3000/api',
  timeout: 1000000,
  headers: {
    'Content-Type': 'application/json'
  }
});

const responseHandler = (response: any) => {
  if(!response.data.success) {
    ElMessage.error(response.data.message);
  }
  return response;
};

apiClient.interceptors.response.use(responseHandler, (error: any) => {
  return Promise.reject(error);
});

export const getUrl = (url: string, data: any) => {
  return new Promise((resolve, reject) => {
    apiClient.get(url, { params: data }).then((res: any) => {
      resolve(res.data);
    }).catch(error => {
      reject(error);
    });
  })
}

export const postUrl = (url: string, data: any) => {
  return new Promise((resolve, reject) => {
    apiClient.post(url, data).then((res: any) => {
      resolve(res.data);
    }).catch(error => {
      reject(error);
    });
  })
}

